<?php
namespace Admin\Model;
use Think\Model;
class AdModel extends Model {

	//	protected $_validate = array(
		//		array('pname','require','广告位名称不得为空！',1), //默认情况下用正则进行验证
			//	array('width','require','广告位宽度不得为空！',1), //默认情况下用正则进行验证
				//array('height','require','广告位高度不得为空！',1), //默认情况下用正则进行验证
				//array('pname','','广告位名称不能重复！',0,'unique',1), // 在新增的时候验证name字段是否唯一
		//);
		
		

		
		
		public function _before_delete($option){
			
			$id=$option['where']['id'];
			$ads=D('ad')->find($id);
			if($ads['type']==1){
				if(file_exists($ads['picurl'])){
					unlink($ads['picurl']);
				}
			}else{
				$adpicres=D('adpic')->where(array('adid'=>$id))->select();
				if($adpicres){
					foreach($adpicres as $k=>$v){
						if(file_exists($v['imgurl'])){
					unlink($v['imgurl']);
				}
					}
					D('adpic')->where(array('adid'=>$id))->delete();
				}
			}
		}
		
		
	

	
	
		public function _after_insert($data,$option){
					if($this->hasImg($_FILES['imgurl']['tmp_name'])){
					 $upload = new \Think\Upload();// 实例化上传类
					 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
					 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
					 $upload->savePath  =      './Public/Uploads/Ad/'; // 设置附件上传目录
					 $upload->rootPath  =      './'; // 设置附件上传目录
					// $upload->autoSub   =     false;// 设置图片上传不生成子目录
					 $info   =   $upload->upload(array('imgurl'=>$_FILES['imgurl'])); 
					 $links=I('links');
					 $adpic=D('adpic');
					//dump($links);die;
					 foreach($info as $k=>$v){
						 $original=$v['rootPath'] . $v['savepath'] . $v['savename']; 	
						 $adpic->add(array(
									'adid'=>$data['id'],
									'imgurl'=>$original,
									'links'=>$links[$k],
						 ));
					 }
							
				}
				
				//处理广告的启用状态
			 if($data['ison']==1){
					 D('ad')->where(array('posid'=>$data['posid']))->setField('ison',0);
				 }
		}
		
		public function _before_update($data,$option){
			//dump($data);die;
			if($_FILES['picurl']['tmp_name']){
				$oldpic=I('oldpicurl');
				if($oldpic){
					if(file_exists($oldpic)){
						unlink($oldpic);
					}
				}
				
				 }
				 //处理广告的启用状态
				 if($data['ison']==1){
					 D('ad')->where(array('posid'=>$data['posid']))->setField('ison',0);
				 }
		}
		
		public function _after_update($data,$option){
		//	dump($data);die;
					if($this->hasImg($_FILES['imgurl']['tmp_name'])){
					 $upload = new \Think\Upload();// 实例化上传类
					 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
					 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
					 $upload->savePath  =      './Public/Uploads/Ad/'; // 设置附件上传目录
					 $upload->rootPath  =      './'; // 设置附件上传目录
					// $upload->autoSub   =     false;// 设置图片上传不生成子目录
					 $info   =   $upload->upload(array('imgurl'=>$_FILES['imgurl'])); 
					 $links=I('links');
					 $adpic=D('adpic');
					//dump($links);die;
					 foreach($info as $k=>$v){
						 $original=$v['rootPath'] . $v['savepath'] . $v['savename']; 	
						 $adpic->add(array(
									'adid'=>$data['id'],
									'imgurl'=>$original,
									'links'=>$links[$k],
						 ));
					 }
							
				}
				
				
				if($this->hasImg($_FILES['old_imgurl']['tmp_name'])){
					 $upload = new \Think\Upload();// 实例化上传类
					 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
					 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
					 $upload->savePath  =      './Public/Uploads/Ad/'; // 设置附件上传目录
					 $upload->rootPath  =      './'; // 设置附件上传目录
					// $upload->autoSub   =     false;// 设置图片上传不生成子目录
					 $info   =   $upload->upload(array('old_imgurl'=>$_FILES['old_imgurl'])); 
				}
				 $links=I('old_links');
				 $adpic=D('adpic');
				 $i=0;
				 foreach($links as $k=>$v){
					 if($info[$i]){
						 $adpic->find($k);
						 unlink($adpic->imgurl);
						 $imgurl=$info['rootPath'] . $info[$i]['savepath'] . $info[$i]['savename']; 	
						 $adpic->where(array('id'=>$k))->save(array(
																												 'links'=>$links[$k],
																												 'imgurl'=>$imgurl,
																												 ));
					 }else{
						   $adpic->where(array('id'=>$k))->save(array('links'=>$links[$k], ));
					 }
						$i++;
						
					 }	
		}	
				
	public function hasImg($files){
			foreach($files as $k=>$v){
				if($v){
					return true;
				}
			}
			return false;
		}
}